package com.yu.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yu.bean.Category;
import com.yu.bean.CategoryExample;
import com.yu.bean.CategoryExample.Criteria;
import com.yu.bean.Info;
import com.yu.bean.InfoDetail;
import com.yu.bean.InfoExample;
import com.yu.bean.InfoExample.Criterion;
import com.yu.bean.Picture;
import com.yu.bean.PictureExample;
import com.yu.dao.CategoryMapper;
import com.yu.dao.InfoMapper;
import com.yu.dao.PictureMapper;

@Service //业务逻辑组件
public class InfoService {
	
	@Autowired
	InfoMapper infoMapper;
	@Autowired
	CategoryMapper categoryMapper;
	@Autowired
	PictureMapper pictureMapper;
	
	/**
	 * 查询所有信息带Admin表
	 * @return
	 */
	public List<Info> getAllInfo() {
		// TODO Auto-generated method stub
		return infoMapper.selectByExampleWithAd(null);
	}

	
	/**
	 * 信息插入，保存
	 * @param info
	 */
	public void saveInfo(Info info) {
		// TODO Auto-generated method stub
		info.setAdId(1);
		/*CategoryExample categoryExample = new CategoryExample();
	    Criteria criteria =categoryExample.createCriteria();
		criteria.andCateNameEqualTo(info.getCategory().getCateName());
		criteria.andCateNameIsNotNull();
		//categoryExample.setOrderByClause();  根据什么升序或者降序查询
		Category category = (Category) categoryMapper.selectByExample(categoryExample);
		info.setCateId(category.getCateId());*/
		info.setInfoBnum(0);
		info.setInfoCnum(0);
		info.setInfoDate(new Date());
		infoMapper.insertSelective(info);
		
	}


	/**
	 * 检验信息title 是否可用
	 * @param infoTitle
	 * return ture 代表infoTitle 可用
	 * return false 代表infoTitle重复不可用
	 */
	public boolean checkInfoTitle(String infoTitle) {
		// TODO Auto-generated method stub
		InfoExample example = new InfoExample();
		com.yu.bean.InfoExample.Criteria criteria = example.createCriteria();
		criteria.andInfoTitleEqualTo(infoTitle);
		//按照条件查询符合条件的个数
		long count = infoMapper.countByExample(example);
		return count == 0;
		
	}


	/**
	 * 根据id查询信息
	 * @param id
	 * @return 
	 */
	public Info getInfoById(Integer id) {
		return infoMapper.selectByPrimaryKeyWithAd(id);
	}


	/**
	 * 根据信息id更新信息
	 * @param info
	 */
	public void updateById(Info info) {
		
		infoMapper.updateByPrimaryKeySelective(info);
		// TODO Auto-generated method stub
		
	}


	/**
	 * 根据信息id删除信息
	 * @param id
	 */
	public void deleteInfoById(Integer id) {
		// TODO Auto-generated method stub
		infoMapper.deleteByPrimaryKey(id);
		
	}


	public void deleteBatch(List<Integer> ids) {
		// TODO Auto-generated method stub
		InfoExample example = new InfoExample();
		com.yu.bean.InfoExample.Criteria criteria = example.createCriteria();
		//delete from xxx where emp_id in (1,2,3)
		criteria.andInfoIdIn(ids);
		infoMapper.deleteByExample(example);
		
		
	}


	/**
	 * 登录成功到首页显示最新资讯根据时间排序
	 * @return
	 */
	public List<Info> getAllInfoOrderByTime() {
		// TODO Auto-generated method stub
		InfoExample infoExample = new InfoExample();
		infoExample.setOrderByClause("info_date DESC");
		List<Info> infos =infoMapper.selectByExampleWithAd(infoExample);
		return infos;
	}
	
	/**
	 * 用户界面的导航栏根据点击的类别显示对应类别的信息
	 * @param cate 
	 * @return
	 */
	public List<Info> getAllInfoByCate(Integer cate) {
		// TODO Auto-generated method stub
		List<Info> infos = new ArrayList<>();
		if (cate != 0) {
			InfoExample infoExample = new InfoExample();
			com.yu.bean.InfoExample.Criteria criteria = infoExample.createCriteria();
			criteria.andCateIdEqualTo(cate);
			infoExample.setOrderByClause("info_date DESC");
			 infos = infoMapper.selectByExampleWithAd(infoExample);
		}else{
			//如果等于零默认为首页的处理没有分类的查询
			InfoExample infoExample2 = new InfoExample();
			infoExample2.setOrderByClause("info_date DESC");
			 infos = infoMapper.selectByExampleWithAd(infoExample2);
		}
		
		return infos;
	}


	/**
	 * 获取每个信息的多段内容和对应图片封装到一个自定义的类中
	 * @param infoId
	 * @return
	 */
	public List<InfoDetail> getInfoDetailById(Integer infoId) {
		// TODO Auto-generated method stub
		Info info =infoMapper.selectByPrimaryKey(infoId);
		PictureExample pictureExample = new PictureExample();
		com.yu.bean.PictureExample.Criteria pCriteria = pictureExample.createCriteria();
		pCriteria.andInfoIdEqualTo(infoId);
		List<Picture> pics = pictureMapper.selectByExample(pictureExample);
		String[] conts =info.getInfoContent().split("-");
		List<InfoDetail> indetials = new ArrayList<>();
		for (int i = 0; i < conts.length; i++) {
			 InfoDetail infoDetial = new InfoDetail();
			if (conts[i] != null && !conts[i].isEmpty()) {
				infoDetial.setCont(conts[i]);
				if (i <pics.size()&& pics.size() != 0) {
					if (pics.get(i).getPicUrl() != null && !pics.get(i).getPicUrl().isEmpty() ) {
						System.out.println("picURL==============================>"+pics.get(i).getPicUrl());
						infoDetial.setPic(pics.get(i).getPicUrl());
					}
					
				}
			}
			indetials.add(infoDetial);
		}
		
		return indetials;
	}


	/**
	 * 根据输入的内容进行模糊查询
	 * @param s
	 * @return
	 */
	public List<Info> searchInfoByCondtion(String s) {
		// TODO Auto-generated method stub
		System.out.println("输入的搜索值为"+s);
		InfoExample infoExample = new InfoExample();
		com.yu.bean.InfoExample.Criteria criteria = infoExample.createCriteria();
		//或者这么搞但是需要自己定义一个andInfoAdNameLike在infoExample中
		criteria.andInfoAdNameDateLike(s);
		
		List<Info> infos = infoMapper.selectByExampleWithAd(infoExample);
		System.out.println("搜索到的结果为"+infos);
		return infos;
	}




}
